package 二叉树相关问题.BST;

import java.util.ArrayList;
import java.util.List;

public class 不同的二叉搜索树2 {
    public List<TreeNode> generateTrees(int n) {
        if (n<1)
            return null;
        return bfs(1,n);
    }

    private List<TreeNode> bfs(int start, int end) {
        List<TreeNode> lists = new ArrayList<>();
        if (start>end){
            lists.add(null);
            return lists;
        }
        for (int i = start; i <= end; i++) {
            List<TreeNode> left = bfs(start, i - 1);
            List<TreeNode> right = bfs(i+1, end);
            for (TreeNode l_left : left) {
                for (TreeNode l_right : right) {
                    TreeNode root = new TreeNode(i);
                    root.left=l_left;
                    root.right=l_right;
                    lists.add(root);
                }
            }
        }
        return lists;
    }
}

